package com.neu.admin;

import com.neu.common.validator.Order;
import com.neu.common.validator.Sort;
import com.neu.domain.ShoppingAd;
import com.neu.service.ShoppingAdService;
import com.neu.util.response.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/ad")
@Api(description = "广告模块（）")
@Validated
public class AdminAdController {

    @Autowired
    private ShoppingAdService shoppingAdService;

    @ApiOperation(value = "获取广告分页数据",notes = "获取广告分页数据和模糊查询")
    @GetMapping("/list")
    public Object list(String name, String content,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {

        List<ShoppingAd> adList = shoppingAdService.querySelective(name, content, page, limit, sort, order);
        return BaseResponse.generateOKListResponseEntity(adList);
    }
    @ApiOperation(value ="删除广告" )
    @PostMapping("/delete")
    public Object delete(@RequestBody ShoppingAd ad) {
            Integer id = ad.getId();
            if (id == null) {
                return BaseResponse.generateBadResponseEntity(500, "删除失败", ad);
            }
            shoppingAdService.deleteById(id);
            return BaseResponse.generateOKResponseEntity();
    }
    @ApiOperation(value = "新增广告")
    @PostMapping("/create")
    public Object create(@RequestBody ShoppingAd ad) {
        try{
            Object error = validate(ad);
            if (error != null) {
                return error;
            }
            shoppingAdService.add(ad);
            return BaseResponse.generateOKResponseEntity(ad);
        }catch (Exception ex){
            ex.printStackTrace();
            return BaseResponse.generateBadResponseEntity();
        }

    }
    @ApiOperation(value = "修改广告")
    @PostMapping("/update")

    public Object update(@RequestBody ShoppingAd ad) {
        try{
            Object error = validate(ad);
            if (error != null) {
                return error;
            }
            if (shoppingAdService.updateById(ad) == 0) {
                return BaseResponse.updatedDataFailed();
            }
            return BaseResponse.generateOKResponseEntity(ad);
        }catch (Exception ex){
            ex.printStackTrace();
            return BaseResponse.generateBadResponseEntity();
        }

    }
    private Object validate(ShoppingAd ad) {
        String name = ad.getName();
        if (StringUtils.isEmpty(name)) {
            return BaseResponse.badArgument();
        }
        String content = ad.getContent();
        if (StringUtils.isEmpty(content)) {
            return BaseResponse.badArgument();
        }
        return null;
    }
}
